package com.roc.admin.infrastructure.generator;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.sql.Types;
import java.util.Collections;

/**
 * MyBatis Plus代码生成器
 */
public class CodeGenerator {

    /**
     * 数据库连接URL
     */
    private static final String URL = "jdbc:mysql://localhost:3306/ddd_demo?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    
    /**
     * 数据库用户名
     */
    private static final String USERNAME = "root";
    
    /**
     * 数据库密码
     */
    private static final String PASSWORD = "root";
    
    /**
     * 代码生成的目标路径
     */
    private static final String OUTPUT_DIR = System.getProperty("user.dir") + "/src/main/java";
    
    /**
     * XML文件生成的目标路径
     */
    private static final String XML_OUTPUT_DIR = System.getProperty("user.dir") + "/src/main/resources/mapper";
    
    /**
     * 作者
     */
    private static final String AUTHOR = "CodeGenerator";
    
    /**
     * 包名
     */
    private static final String PACKAGE_NAME = "com.roc.admin";
    
    /**
     * 表前缀
     */
    private static final String TABLE_PREFIX = "sys_";

    /**
     * 执行代码生成
     *
     * @param args 命令行参数
     */
    public static void main(String[] args) {
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                // 全局配置
                .globalConfig(builder -> {
                    builder.author(AUTHOR)
                            .enableSwagger()
                            .outputDir(OUTPUT_DIR)
                            .commentDate("yyyy-MM-dd")
                            .fileOverride();
                })
                // 包配置
                .packageConfig(builder -> {
                    builder.parent(PACKAGE_NAME)
                            .entity("infrastructure.persistence")
                            .service("application.service")
                            .serviceImpl("application.service.impl")
                            .mapper("infrastructure.mapper")
                            .controller("interfaces.controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, XML_OUTPUT_DIR));
                })
                // 策略配置
                .strategyConfig(builder -> {
                    builder.addInclude("sys_user", "sys_role", "sys_menu", "sys_dept", "sys_user_role", "sys_role_menu", "sys_role_dept")
                            .addTablePrefix(TABLE_PREFIX)
                            // Entity策略配置
                            .entityBuilder()
                            .enableLombok()
                            .enableChainModel()
                            .enableTableFieldAnnotation()
                            .formatFileName("%sPO")
                            // Controller策略配置
                            .controllerBuilder()
                            .enableRestStyle()
                            .formatFileName("%sController")
                            // Service策略配置
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            // Mapper策略配置
                            .mapperBuilder()
                            .enableMapperAnnotation()
                            .formatMapperFileName("%sMapper")
                            .formatXmlFileName("%sMapper");
                })
                // 数据类型转换配置
                .templateConfig(builder -> {
                    builder.entity("/templates/entity.java.ftl")
                            .service("/templates/service.java.ftl")
                            .serviceImpl("/templates/serviceImpl.java.ftl")
                            .mapper("/templates/mapper.java.ftl")
                            .controller("/templates/controller.java.ftl")
                            .xml("/templates/mapper.xml.ftl");
                })
                // 自定义数据类型转换
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
